#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/platform_device.h>
#include <linux/wait.h>
#include <linux/list.h>
#include <linux/fsl_devices.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/workqueue.h>
#include <linux/sizes.h>
#include <linux/types.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include<linux/slab.h>

/*
	backlight {
		compatible = "pwm-backlight";
		pwms = <&pwm1 0 5000000>;
		brightness-levels = <0 4 8 16 32 64 128 255>;
		default-brightness-level = <7>;
		status = "okay";
	};
*/
static int __init mydrv_init(void)
{
    int ret;
    struct device_node* node = NULL;
    struct property *prop = NULL; 
    u32 temp;
    u8 ele , i;
    unsigned int* arr_addr;

    const char* str;  

    node = of_find_node_by_path("/backlight");
    if(node == NULL){
        ret = -EINVAL;
        goto fail_node;
    }

    prop = of_find_property(node, "compatible", NULL);
    if(prop == NULL){
        goto fail_readstr;
    }else{
        printk("of_find_property:%s\n",(char*)prop -> value);
    }

    ret = of_property_read_string(node, "compatible", &str);
    if(ret < 0){
        goto fail_readstr;
    }else{
        printk("of_property_read_string:%s\n",str);
    }

    ret = of_property_read_u32(node,"default-brightness-level", &temp);
    if(ret < 0){
        goto fail_readu32;
    }else{
        printk("of_property_read_u32:%d\n",temp);
    }    

    ele = of_property_count_elems_of_size(node, "brightness-levels", sizeof(u32));
    if(ele < 0){
        ret = -EINVAL;
        goto fail_ele;
    }else{
        printk("of_property_count_elems_of_size: %d\n", ele);
    }

    arr_addr = (u32 *)kmalloc(ele * sizeof(u32), GFP_KERNEL);
    if(arr_addr == NULL){
        ret = -1;
        goto fail_mem;
    }

    ret = of_property_read_u32_array(node, "brightness-levels", arr_addr, ele);
    if(ret < 0){
        goto fail_read_arr;
    }else{
        printk("brightness-levels:\n");
        for(i = 0;i < ele; i++){
            printk("arr[%d]:%d ",i, *(arr_addr + i));
        }
        printk("\n");
    }

    kfree(arr_addr);
    return 0;


fail_read_arr:
fail_mem:
    kfree(arr_addr);
fail_ele:
fail_node:
fail_readu32:
fail_readstr:
    return ret;
}

static void __exit mydrv_exit(void)
{

}

module_init(mydrv_init);
module_exit(mydrv_exit);
MODULE_LICENSE("GPL");